package com.example.linuxchat.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;

import com.example.linuxchat.ChatActivity;
import com.example.linuxchat.MainActivity;
import com.example.linuxchat.UserInfoActivity;
import com.example.linuxchat.bean.User;
import com.tencent.connect.UserInfo;
import com.tencent.connect.auth.QQToken;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author liulingjie
 * @date 20-6-18 下午2:40
 */
public class LoginBack implements IUiListener {
    private Tencent tencent;
    String USER_ID;
    public LoginBack( Tencent tencent){
        this.tencent=tencent;
    }

    //获取到授权码调用
    @Override
    public void onComplete(Object o) {
//        Toast.makeText(MainActivity.the,"授权成功",Toast.LENGTH_SHORT).show();
        try {
            JSONObject obj = new JSONObject(o.toString());
            USER_ID = obj.getString("openid");
            String accessToken = obj.getString("access_token");
            String expires = obj.getString("expires_in");
            Log.d("showuserinfo",o.toString());
            tencent.setOpenId(USER_ID);
            tencent.setAccessToken(accessToken, expires);
            QQToken qqToken = tencent.getQQToken();

            UserInfo user = new UserInfo(MainActivity.the.getApplicationContext(), qqToken);
            //获取用户信息调用
            user.getUserInfo(new IUiListener() {
                @Override
                public void onComplete(Object response) {
                    MainActivity.userjson=response.toString();

//                    Toast.makeText(MainActivity.the,"获取用户信息成功",Toast.LENGTH_SHORT).show();
                    User user=new User();
                    try {
                        Log.d("showuserinfo",response.toString());
                        JSONObject json=new JSONObject(response.toString());
                        user.setId(USER_ID);
                        user.setNickname(json.getString("nickname"));
                        user.setGender(json.getString("gender"));
                        user.setCity(json.getString("city"));
                        user.setIsYellow(json.getString("is_yellow_vip"));
                        user.setProvince(json.getString("province"));
                        user.setVip(json.getString("vip"));
                        user.setYear(json.getString("year"));
                        user.setUrl(json.getString("figureurl_qq_2"));

                        SharedPreferences sp = MainActivity.the.getSharedPreferences("user_info", Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putString("id",user.getId());
                        editor.putString("nickname",user.getNickname());
                        editor.putString("gender",user.getGender());
                        editor.putString("city",user.getCity());
                        editor.putString("is_yellow_vip",user.getIsYellow());
                        editor.putString("province",user.getProvince());
                        editor.putString("vip",user.getVip());
                        editor.putString("year",user.getYear());
                        editor.putString("url",user.getUrl());
                        editor.commit();

                        //记录登入对象
                        MainActivity.user=user;
                        //跳转显示信息
                        Intent intent=new Intent(MainActivity.the, ChatActivity.class);
                        intent.putExtra("user",user);
                        MainActivity.the.startActivity(intent);
                        MainActivity.the.finish();

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }

                @Override
                public void onError(UiError uiError) {
                    Toast.makeText(MainActivity.the,"获取信息失败",Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onCancel() {
                    Toast.makeText(MainActivity.the,"取消获取信息",Toast.LENGTH_SHORT).show();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
    @Override
    public void onError(UiError uiError) {
        Toast.makeText(MainActivity.the,"授权失败",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCancel() {
        Toast.makeText(MainActivity.the,"取消登入",Toast.LENGTH_SHORT).show();
    }

}
